package com.fancy.learncenter.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.NGKListDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.ui.activity.CourseDetailActivity;
import com.fancy.learncenter.ui.activity.PlayAudioActivity;
import com.fancy.learncenter.ui.activity.PlayVedioActivity;
import com.fancy.learncenter.ui.activity.base.MallCommonH5Activity;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by Hyy on 2016/12/21.
 */

public class FragmentNGKRVAdapter extends CommonRecycleViewAdapter<NGKListDataBean.RecommendListBean> {
    Context context;
    List<NGKListDataBean.RecommendListBean> itemDatas;

    public FragmentNGKRVAdapter(Context mContext, List<NGKListDataBean.RecommendListBean> itemDatas) {
        super(mContext, R.layout.fragment_ngk_rv_root_item, itemDatas);
        this.context = mContext;
        this.itemDatas = itemDatas;
    }


    @Override
    public void bindView(CustomViewHold customViewHold, final NGKListDataBean.RecommendListBean item, int position) {
        if (getAdapterItemViewType(position) == TYPE_IMG) {
        } else {
            TextView title = customViewHold.getView(R.id.title);
            title.setText(item.getName());

            TextView pv_num = customViewHold.getView(R.id.pv_num);
            pv_num.setText(item.getPageViewNums() + "次浏览");

            TextView praise_num = customViewHold.getView(R.id.praise_num);
            praise_num.setText(item.getStatisticString());
        }

        SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
        simpleDraweeView.setImageURI(item.getCoverImgUrl());


        customViewHold.getRoorView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                 entityType	Integer	业务类型	1 视频 2 音频  6 课程
//                 frontType	Integer	前端类型	1 原生 2 H5

                if (item.getFrontType() == 1){

                    if (item.getEntityType() == 1){  //视频
                        Intent intent = new Intent(context, PlayVedioActivity.class);
                        intent.putExtra(Constant.showResourceCoverImg, item.getCoverImgUrl());
                        intent.putExtra(Constant.playLink, "http://img1.fancyedu.com" +item.getLinkUrl());  //特别标记，返回参数没有域名
                        intent.putExtra(Constant.showResourceId, item.getEntityId());
                        intent.putExtra(Constant.showResourceName, item.getName());
//                        intent.putExtra(Constant.intro, item.getName());?
                        context.startActivity(intent);
                    }else if (item.getEntityType() == 2){ //音频
                        Intent intent = new Intent(context, PlayAudioActivity.class);
                        intent.putExtra(Constant.showResourceId,item.getEntityId());
                        intent.putExtra(Constant.playLink, "http://img1.fancyedu.com"+item.getLinkUrl());
                        intent.putExtra(Constant.showResourceCoverImg, item.getCoverImgUrl());
                        context.startActivity(intent);
                    }else if (item.getEntityType() == 6){  //课程
                        Intent intent = new Intent(context, CourseDetailActivity.class);
                        intent.putExtra("type",item.getEntityId());
                        context.startActivity(intent);
                    }

                }else if (item.getFrontType() == 2){  //h5
                    Intent intent = new Intent(context, MallCommonH5Activity.class);
                    intent.putExtra(MallCommonH5Activity.URL_KEY, item.getLinkUrl());
                    context.startActivity(intent);
                }

            }
        });

    }


    int TYPE_IMG = 2;
    int TYPE_RV = 1;


    @Override
    public int getAdapterItemViewType(int position) {
        if (itemDatas.get(position).getShowType() == 1) {
            return TYPE_RV;
        } else {
            return TYPE_IMG;
        }
    }

    @Override
    public CustomViewHold onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        if (viewType == TYPE_RV) {
            this.resource = R.layout.fragment_ngk_rv_root_item;
        } else {
            this.resource = R.layout.fragment_ngk_rv_img_item;
        }
        return super.onCreateViewHolder(parent, viewType, isItem);
    }


    class RootItemAdapter extends CommonRecycleViewAdapter<String> {

        public RootItemAdapter(Context mContext, List<String> itemDatas) {
            super(mContext, R.layout.fragment_ngk_rv_root_item, itemDatas);
        }

        @Override
        public void bindView(CustomViewHold customViewHold, String item, int position) {

        }
    }
}
